<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Projection &mdash; Python GDAL/OGR Cookbook 1.0 documentation</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Python GDAL/OGR Cookbook 1.0 documentation" href="index.html" />
    <link rel="next" title="API Tricks and Trapdoors" href="gotchas.html" />
    <link rel="prev" title="Raster Layers" href="raster_layers.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="gotchas.html" title="API Tricks and Trapdoors"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="raster_layers.html" title="Raster Layers"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Python GDAL/OGR Cookbook 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="projection">
<h1>Projection<a class="headerlink" href="#projection" title="Permalink to this headline">¶</a></h1>
<div class="section" id="create-projection">
<h2>Create Projection<a class="headerlink" href="#create-projection" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">osr</span>
<span class="n">spatialRef</span> <span class="o">=</span> <span class="n">osr</span><span class="o">.</span><span class="n">SpatialReference</span><span class="p">()</span>
<span class="n">spatialRef</span><span class="o">.</span><span class="n">ImportFromEPSG</span><span class="p">(</span><span class="mi">2927</span><span class="p">)</span>         <span class="c"># from EPSG</span>
</pre></div>
</div>
</div>
<div class="section" id="reproject-a-geometry">
<h2>Reproject a Geometry<a class="headerlink" href="#reproject-a-geometry" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">osr</span>

<span class="n">source</span> <span class="o">=</span> <span class="n">osr</span><span class="o">.</span><span class="n">SpatialReference</span><span class="p">()</span>
<span class="n">source</span><span class="o">.</span><span class="n">ImportFromEPSG</span><span class="p">(</span><span class="mi">2927</span><span class="p">)</span>

<span class="n">target</span> <span class="o">=</span> <span class="n">osr</span><span class="o">.</span><span class="n">SpatialReference</span><span class="p">()</span>
<span class="n">target</span><span class="o">.</span><span class="n">ImportFromEPSG</span><span class="p">(</span><span class="mi">4326</span><span class="p">)</span>

<span class="n">transform</span> <span class="o">=</span> <span class="n">osr</span><span class="o">.</span><span class="n">CoordinateTransformation</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">target</span><span class="p">)</span>

<span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="s">&quot;POINT (1120351.57 741921.42)&quot;</span><span class="p">)</span>
<span class="n">point</span><span class="o">.</span><span class="n">Transform</span><span class="p">(</span><span class="n">transform</span><span class="p">)</span>

<span class="k">print</span> <span class="n">point</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="get-projection">
<h2>Get Projection<a class="headerlink" href="#get-projection" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span><span class="p">,</span> <span class="n">osr</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;ESRI Shapefile&#39;</span><span class="p">)</span>
<span class="n">dataset</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">r&#39;c:\data\yourshpfile.shp&#39;</span><span class="p">)</span>

<span class="c"># from Layer</span>
<span class="n">layer</span> <span class="o">=</span> <span class="n">dataset</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>
<span class="n">spatialRef</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetSpatialRef</span><span class="p">()</span>
<span class="c"># from Geometry</span>
<span class="n">feature</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetNextFeature</span><span class="p">()</span>
<span class="n">geom</span> <span class="o">=</span> <span class="n">feature</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span>
<span class="n">spatialRef</span> <span class="o">=</span> <span class="n">geom</span><span class="o">.</span><span class="n">GetSpatialReference</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="reproject-a-layer">
<h2>Reproject a Layer<a class="headerlink" href="#reproject-a-layer" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span><span class="p">,</span> <span class="n">osr</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;ESRI Shapefile&#39;</span><span class="p">)</span>

<span class="c"># input SpatialReference</span>
<span class="n">inSpatialRef</span> <span class="o">=</span> <span class="n">osr</span><span class="o">.</span><span class="n">SpatialReference</span><span class="p">()</span>
<span class="n">inSpatialRef</span><span class="o">.</span><span class="n">ImportFromEPSG</span><span class="p">(</span><span class="mi">2927</span><span class="p">)</span>

<span class="c"># output SpatialReference</span>
<span class="n">outSpatialRef</span> <span class="o">=</span> <span class="n">osr</span><span class="o">.</span><span class="n">SpatialReference</span><span class="p">()</span>
<span class="n">outSpatialRef</span><span class="o">.</span><span class="n">ImportFromEPSG</span><span class="p">(</span><span class="mi">4326</span><span class="p">)</span>

<span class="c"># create the CoordinateTransformation</span>
<span class="n">coordTrans</span> <span class="o">=</span> <span class="n">osr</span><span class="o">.</span><span class="n">CoordinateTransformation</span><span class="p">(</span><span class="n">inSpatialRef</span><span class="p">,</span> <span class="n">outSpatialRef</span><span class="p">)</span>

<span class="c"># get the input layer</span>
<span class="n">inDataSet</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">r&#39;c:\data\spatial\basemap.shp&#39;</span><span class="p">)</span>
<span class="n">inLayer</span> <span class="o">=</span> <span class="n">inDataSet</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>

<span class="c"># create the output layer</span>
<span class="n">outputShapefile</span> <span class="o">=</span> <span class="s">r&#39;c:\data\spatial\basemap_4326.shp&#39;</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">outputShapefile</span><span class="p">):</span>
    <span class="n">driver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="n">outputShapefile</span><span class="p">)</span>
<span class="n">outDataSet</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="n">outputShapefile</span><span class="p">)</span>
<span class="n">outLayer</span> <span class="o">=</span> <span class="n">outDataSet</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="s">&quot;basemap_4326&quot;</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbMultiPolygon</span><span class="p">)</span>

<span class="c"># add fields</span>
<span class="n">inLayerDefn</span> <span class="o">=</span> <span class="n">inLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">inLayerDefn</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()):</span>
    <span class="n">fieldDefn</span> <span class="o">=</span> <span class="n">inLayerDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="n">outLayer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">fieldDefn</span><span class="p">)</span>

<span class="c"># get the output layer&#39;s feature definition</span>
<span class="n">outLayerDefn</span> <span class="o">=</span> <span class="n">outLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>

<span class="c"># loop through the input features</span>
<span class="n">inFeature</span> <span class="o">=</span> <span class="n">inLayer</span><span class="o">.</span><span class="n">GetNextFeature</span><span class="p">()</span>
<span class="k">while</span> <span class="n">inFeature</span><span class="p">:</span>
    <span class="c"># get the input geometry</span>
    <span class="n">geom</span> <span class="o">=</span> <span class="n">inFeature</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span>
    <span class="c"># reproject the geometry</span>
    <span class="n">geom</span><span class="o">.</span><span class="n">Transform</span><span class="p">(</span><span class="n">coordTrans</span><span class="p">)</span>
    <span class="c"># create a new feature</span>
    <span class="n">outFeature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">outLayerDefn</span><span class="p">)</span>
    <span class="c"># set the geometry and attribute</span>
    <span class="n">outFeature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">geom</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">outLayerDefn</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()):</span>
        <span class="n">outFeature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="n">outLayerDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetNameRef</span><span class="p">(),</span> <span class="n">inFeature</span><span class="o">.</span><span class="n">GetField</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
    <span class="c"># add the feature to the shapefile</span>
    <span class="n">outLayer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">outFeature</span><span class="p">)</span>
    <span class="c"># dereference the features and get the next input feature</span>
    <span class="n">outFeature</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">inFeature</span> <span class="o">=</span> <span class="n">inLayer</span><span class="o">.</span><span class="n">GetNextFeature</span><span class="p">()</span>

<span class="c"># Save and close the shapefiles</span>
<span class="n">inDataSet</span> <span class="o">=</span> <span class="bp">None</span>
<span class="n">outDataSet</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</div>
</div>
<div class="section" id="export-projection">
<h2>Export Projection<a class="headerlink" href="#export-projection" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span><span class="p">,</span> <span class="n">osr</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;ESRI Shapefile&#39;</span><span class="p">)</span>
<span class="n">dataset</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">r&#39;c:\data\yourshpfile.shp&#39;</span><span class="p">)</span>
<span class="n">layer</span> <span class="o">=</span> <span class="n">dataset</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>
<span class="n">spatialRef</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetSpatialRef</span><span class="p">()</span>

<span class="n">spatialRef</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
<span class="n">spatialRef</span><span class="o">.</span><span class="n">ExportToPrettyWkt</span><span class="p">()</span>
<span class="n">spatialRef</span><span class="o">.</span><span class="n">ExportToPCI</span><span class="p">()</span>
<span class="n">spatialRef</span><span class="o">.</span><span class="n">ExportToUSGS</span><span class="p">()</span>
<span class="n">spatialRef</span><span class="o">.</span><span class="n">ExportToXML</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="create-an-esri-prj-file">
<h2>Create an ESRI.prj file<a class="headerlink" href="#create-an-esri-prj-file" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span><span class="p">,</span> <span class="n">osr</span>

<span class="n">spatialRef</span> <span class="o">=</span> <span class="n">osr</span><span class="o">.</span><span class="n">SpatialReference</span><span class="p">()</span>
<span class="n">spatialRef</span><span class="o">.</span><span class="n">ImportFromEPSG</span><span class="p">(</span><span class="mi">26912</span><span class="p">)</span>

<span class="n">spatialRef</span><span class="o">.</span><span class="n">MorphToESRI</span><span class="p">()</span>
<span class="nb">file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;yourshpfile.prj&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">spatialRef</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">())</span>
<span class="nb">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Projection</a><ul>
<li><a class="reference internal" href="#create-projection">Create Projection</a></li>
<li><a class="reference internal" href="#reproject-a-geometry">Reproject a Geometry</a></li>
<li><a class="reference internal" href="#get-projection">Get Projection</a></li>
<li><a class="reference internal" href="#reproject-a-layer">Reproject a Layer</a></li>
<li><a class="reference internal" href="#export-projection">Export Projection</a></li>
<li><a class="reference internal" href="#create-an-esri-prj-file">Create an ESRI.prj file</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="raster_layers.html"
                        title="previous chapter">Raster Layers</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="gotchas.html"
                        title="next chapter">API Tricks and Trapdoors</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/projection.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="gotchas.html" title="API Tricks and Trapdoors"
             >next</a> |</li>
        <li class="right" >
          <a href="raster_layers.html" title="Raster Layers"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Python GDAL/OGR Cookbook 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2013, Jared Erickson, Cort Daniel, Michael Payne.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
    </div>
  </body>
</html>